package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.query.TeacherQuery;
import com.atguigu.eduservice.service.EduTeacherService;
import com.atguigu.servicebase.handler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-06-25
 * 阿里云 1ID 2Secret
 * LTAI4GKVG6ZjcLpmQpWoMFmg
 * EAsw6cjn7j4YcrshYZ6SGTdsyk51pC
 */
@Api(tags = "教师管理")
@RestController
@RequestMapping("/eduservice/eduteacher")
@CrossOrigin//跨域 跨域问题：协议、地址、端口
//nginx反向代理   http://localhost:8001/eduservice/edu-teacher
public class EduTeacherController {

    @Autowired
    private EduTeacherService teacherService;

    @GetMapping
    @ApiOperation("获取所有教师信息")
    @ApiOperationSupport(author = "zqk", order = 1)
    public R list() {
        //        int i = 10/0;//测试统一异常处理的，不让错误返回给页面
        try {
            int a = 10/0;
        }catch(Exception e) {
            throw new GuliException(20001,"出现自定义类的异常");
        }
        List<EduTeacher> list = teacherService.list(null);
        return R.ok().data("items", list);
    }


    @DeleteMapping("{id}")
    @ApiOperation("教师逻辑删除   ---》在MyBatisPlusConfig配置类写插件实体类加注解")
    @ApiOperationSupport(author = "zqk", order = 2)
    public R removeById(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable("id") String id) {
        boolean remove = teacherService.removeById(id);
        if (remove) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @GetMapping("{page}/{limit}")
    @ApiOperation("分页讲师列表")
    @ApiOperationSupport(author = "zqk", order = 3)
    public R pageList(
            @ApiParam(name = "page", value = "当前页", required = true)
            @PathVariable("page") Long page,
            @ApiParam(name = "limit", value = "每页查询数量", required = true)
            @PathVariable("limit") Long limit
    ) {
        Page<EduTeacher> pageParam = new Page<EduTeacher>(page, limit);

        teacherService.page(pageParam, null);
        List<EduTeacher> records = pageParam.getRecords();//分页的所有方法
        long total = pageParam.getTotal();//总条数
        //1 数据存入map
        //        Map<String,Object> map = new HashMap<>();
        //        map.put("list",records);
        //        map.put("total",total);
        //        return R.ok().data(map);
        //2直接拼接
        return R.ok().data("list", records).data("total", total);

    }

    @PostMapping("getTeacherPageVo/{current}/{limit}")//用PostMapping 才能用@RequestBody
    @ApiOperation("分页讲师列表带条件")
    @ApiOperationSupport(author = "zqk", order = 4)
    public R getTeacherPageVo(
            @ApiParam(name = "current", value = "当前页", required = true)
            @PathVariable("current") Long current,
            @ApiParam(name = "limit", value = "每页查询数量", required = true)
            @PathVariable("limit") Long limit,
            @ApiParam(name = "teacherQuery", value = "查询对象", required = false)
            @RequestBody TeacherQuery teacherQuery) {//用PostMapping 才能用@RequestBody
        //@RequestBody 把json串转成实体类

        Page<EduTeacher> pageParam = new Page<>(current, limit);

        teacherService.getTeacherPage(pageParam, teacherQuery);
        List<EduTeacher> records = pageParam.getRecords();//分页的所有方法
        long total = pageParam.getTotal();//总条数
        //1 数据存入map
        //        Map<String,Object> map = new HashMap<>();
        //        map.put("list",records);
        //        map.put("total",total);
        //        return R.ok().data(map);
        //2直接拼接
        return R.ok().data("list", records).data("total", total);

    }


    @PostMapping("addTeacher")
    @ApiOperation("添加讲师")
    @ApiOperationSupport(author = "zqk", order = 5)
    public R addTeacher(
            @ApiParam(name = "eduTeacher", value = "讲师对象", required = true)
            @RequestBody EduTeacher eduTeacher) {
        boolean save = teacherService.save(eduTeacher);
        if (save) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    //业务描述， 要修改的话要，先查询 ，才能修改
    @GetMapping("getTeacherById/{id}")
    @ApiOperation("根据ID查询讲师")
    @ApiOperationSupport(author = "zqk", order = 6)
    public R getTeacherById(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable String id) {

        EduTeacher eduTeacher = teacherService.getById(id);
        return R.ok().data("eduTeacher", eduTeacher);

    }

    @ApiOperation(value = "修改讲师")
    @PostMapping("updateTeacher")//都是post不是put 是为了添加的时候做判断
    @ApiOperationSupport(author = "zqk", order = 6/1)
    public R updateTeacher(@RequestBody EduTeacher eduTeacher){
        boolean update = teacherService.updateById(eduTeacher);
        if(update){
            return R.ok();
        }else{
            return R.error();
        }

    }

    @PutMapping("updateTeacherById/{id}")
    @ApiOperation("根据ID更新讲师")
    @ApiOperationSupport(author = "zqk", order = 7)
    public R updateTeacher(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable("id") String id,

            @ApiParam(name = "teacher", value = "讲师对象", required = true)
            @RequestBody EduTeacher eduTeacher) {
        {

            eduTeacher.setId(id);
            teacherService.updateById(eduTeacher);
            return R.ok();

        }

    }
}

